package com.wsz.eduservice.controller;


import com.wsz.commonutils.R;
import com.wsz.eduservice.client.VodClient;
import com.wsz.eduservice.pojo.Video;
import com.wsz.eduservice.service.VideoService;
import com.wsz.servicebase.exceptionhandler.MyTestException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "小节管理")
@RestController
@RequestMapping("/eduservice/video")
public class EduVideoController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private VodClient vodClient;

    @ApiOperation("增加小节")
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody Video video) {
        System.out.println(video);
        videoService.save(video);
        return R.ok();
    }
    @ApiOperation("删除小节")
    @DeleteMapping("/{id}")
    public R deleteVideo(@PathVariable String id) {

        Video byId = videoService.getById(id);
        String courseId =byId.getCourseId();

        if(StringUtils.isNotBlank(courseId)) {
            R r = vodClient.removeAlyVideo(courseId);
            if(r.getCode() == 20001){
                throw new MyTestException(20001,"删除视频失败了！！！");
            }
        }

        videoService.removeById(id);
        return R.ok();
    }

    @ApiOperation("修改小节")
    @PostMapping("/updateVideo")
    public R updateVideo(@RequestBody Video video) {
        videoService.updateById(video);
        return R.ok();
    }
    @ApiOperation("根据小节Id获取小节信息")
    @GetMapping("/getVideo/{videoId}")
    public R getVideo(@PathVariable(name="videoId") String Id){
        Video video = videoService.getById(Id);
        return R.ok().data("video",video);
    }
}
